package com.yang.springboot.web;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;

import java.util.*;

/**
 * @author yanwei.yang
 * @date 2021/12/27 14:41
 * @description:
 */
public class DateUtilTest {


    private static Map<Integer, List<String>> getDate(String start, String end) {

        Map<Integer, List<String>> map = new IdentityHashMap<>();
        DateTime startDate = DateUtil.parse(start);
        DateTime endDate = DateUtil.parse(end);
        long betweenDay = DateUtil.between(startDate, endDate, DateUnit.DAY);
        for (int i = 0; i < betweenDay; i++) {
            DateTime dt = DateUtil.offsetDay(startDate, i);
            int w = (DateUtil.dayOfWeek(dt) - 1);
            String str = dt.toString("yyyy-MM-dd");
            List<String> list = map.get(w);
            if (list != null) {
                final boolean present = list.stream().anyMatch(c -> c.equals(str));
                if (!present) {
                    list.add(str);
                    map.put(w, list);
                }
            } else {
                map.put(w, new ArrayList<>(Collections.singletonList(str)));
            }


        }
        return map;
    }

    public static void main(String[] args) {
        Map<Integer, List<String>> date = getDate("2021-12-27", "2022-01-27");
        System.out.println(JSON.toJSONString(date));
    }
}